package com.module.admin.app.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.app.dao.AppApiTestDao;
import com.module.admin.app.pojo.AppApiTest;
import com.module.admin.app.pojo.AppApiTestDtl;
import com.module.admin.app.service.AppApiTestDtlService;
import com.module.admin.app.service.AppApiTestService;
import com.system.comm.model.KvEntity;
import com.system.comm.model.Page;
import com.system.comm.utils.FrameNoUtil;
import com.system.comm.utils.FrameStringUtil;
import com.system.handle.model.ResponseFrame;

/**
 * app_api_test的Service
 * @author admin
 * @date 2020-12-28 16:57:41
 * @version V1.0.0
 */
@Component
public class AppApiTestServiceImpl implements AppApiTestService {

	@Autowired
	private AppApiTestDao appApiTestDao;
	@Autowired
	private AppApiTestDtlService appApiTestDtlService;
	
	/**
	 * 保存或修改
	 * @param appApiTest
	 * @return
	 */
	@Override
	public ResponseFrame saveOrUpdate(AppApiTest appApiTest) {
		ResponseFrame frame = new ResponseFrame();
		if(FrameStringUtil.isEmpty(appApiTest.getAatId())) {
			appApiTest.setAatId(FrameNoUtil.uuid());
			appApiTestDao.save(appApiTest);
			// 为复制
			if (FrameStringUtil.isNotEmpty(appApiTest.getCopyAatId())) {
				List<AppApiTestDtl> list = appApiTestDtlService.findByAatId(appApiTest.getCopyAatId());
				for (AppApiTestDtl appApiTestDtl : list) {
					appApiTestDtl.setAatdId(null);
					appApiTestDtl.setAatId(appApiTest.getAatId());
					appApiTestDtlService.saveOrUpdate(appApiTestDtl);
				}
			}
			
		} else {
			appApiTestDao.update(appApiTest);
		}
		frame.setSucc();
		return frame;
	}
	
	/**
	 * 根据aatId获取对象
	 * @param aatId
	 * @return
	 */
	@Override
	public AppApiTest get(String aatId) {
		return handleDtl(appApiTestDao.get(aatId));
	}

	/**
	 * 分页获取对象
	 * @param appApiTest
	 * @return
	 */
	@Override
	public Page<AppApiTest> pageQuery(AppApiTest appApiTest) {
		appApiTest.setDefPageSize();
		int total = appApiTestDao.findAppApiTestCount(appApiTest);
		List<AppApiTest> data = null;
		if(total > 0) {
			data = appApiTestDao.findAppApiTest(appApiTest);
			for (AppApiTest item : data) {
				handleDtl(item);
			}
		}
		Page<AppApiTest> page = new Page<AppApiTest>(appApiTest.getPage(), appApiTest.getSize(), total, data);
		return page;
	}
	/**
	 * 处理字典等详细信息的方法
	 * @param appApiTest
	 */
	private AppApiTest handleDtl(AppApiTest appApiTest) {
		if (appApiTest != null) {
		}
		return appApiTest;
	}
	
	/**
	 * 根据aatId删除对象
	 * @param aatId
	 * @return
	 */
	@Override
	public ResponseFrame delete(String aatId) {
		ResponseFrame frame = new ResponseFrame();
		appApiTestDao.delete(aatId);
		frame.setSucc();
		return frame;
	}

	@Override
	public List<KvEntity> findSearch(String searchString) {
		return appApiTestDao.findSearch(searchString);
	}
}